
<template>
  <div>
    <el-dialog
      :title="title"
      class="gl_dialog_TitleLine gl_dl_footer"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="632px"
      top="25vh"
      :before-close="clickCancel"
    >
      <el-scrollbar ref="scroll" v-loading="loading" wrap-class="scrollbar-wrapper">
        <el-form ref="selectedMsg" class="gl_form_mini" :model="selectedMsg" :rules="selectedMsg_Rules" label-position="top" label-width="120px" style="padding:0 30px">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="快递名称" prop="name">
                <el-input v-model="selectedMsg.name" placeholder="请输入" maxlength="30" type="text" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-scrollbar>

      <span slot="footer" class="dialog-footer">
        <!-- 取消按钮 -->
        <el-button size="mini" @click="clickCancel">{{ $t('button.cancel') }}</el-button>
        <!-- 编辑的保存按钮 -->
        <el-button v-if="type === 'edit'" type="primary" size="mini" @click="clickEdit()">确 定</el-button>
        <!-- 新增的保存按钮 -->
        <el-button v-else type="primary" size="mini" @click="clickAdd()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { SiSTKWMallCreateStoreExpressInput } from '@/lib/form/shoppingmall/swaggerForm';
import validator from '@/lib/validate/index.js';
import {
  api_POST_MallStoreExpress,
  api_PUT_MallStoreExpressId
} from '@/extend/api/shoppingmallswaggerApi/app';
export default {
  props: {
    storeId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      selectedMsg: new SiSTKWMallCreateStoreExpressInput(),
      selectedMsg_Rules: {
        name: [
          validator.required('快递名称不能为空')
        ]
      },
      title: '',
      loading: false,
      dialogVisible: false,
      type: '', // 弹框状态，是新增和是修改 new 是新增，edit是修改
      rowId: '' // 编辑时候的id
    };
  },
  watch: {
    'type': {
      handler: function(val) {
        this.title = val === 'edit' ? '编辑快递' : '新增快递';
      }
    }
  },
  methods: {
    clickEdit() {
      if (!this.selectedMsg.name) return;
      this.loading = true;
      api_PUT_MallStoreExpressId(this.rowId, this.selectedMsg).then(res => {
        this.loading = false;
        this.$message({ type: 'success', message: '更新成功' });
        this.selectedMsg = new SiSTKWMallCreateStoreExpressInput();
        this.dialogVisible = false;
        this.$parent.getPageList();
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickAdd() {
      this.$refs.selectedMsg.validate(valid => {
        this.submitLoading = true;
        if (!valid) {
          return false;
        }
      });
      if (!this.selectedMsg.name) return;
      this.selectedMsg.storeId = this.storeId;
      this.loading = true;
      api_POST_MallStoreExpress(this.selectedMsg).then(res => {
        this.loading = false;
        if (res) {
          this.$message({ type: 'success', message: '添加成功' });
          this.dialogVisible = false;
          this.selectedMsg = new SiSTKWMallCreateStoreExpressInput();
          this.$parent.getPageList();
        }
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickCancel() {
      this.selectedMsg = new SiSTKWMallCreateStoreExpressInput();
      this.dialogVisible = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.el-dialog {
  display: flex;
  flex-direction: column;
  margin:0 !important;
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%);
}
</style>
